﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Tumblr.API;
using TumblrPlugin.Resources;

namespace TumblrPlugin.Settings
{
    public partial class SettingsForm : Form
    {
        private readonly XmlDocument _persistXml;

        public SettingsForm(XmlDocument persistXml)
        {
            

            InitializeComponent();
            Icon = Icon.FromHandle(Resource_common.tumblrIcon.GetHicon());
            _persistXml = persistXml;

            if (_persistXml != null)
            {
                var accountSettings = AccountSettings.Deserialize(persistXml);
                txtPassword.Text = accountSettings.Password;
                txtEmail.Text = accountSettings.Email;
                chkRememberCredentials.Checked = accountSettings.RememberCredentials;
            }
        }

        private void publishCancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void publishNextButton_Click(object sender, EventArgs e)
        {
            var accountSettings = new AccountSettings();
            if (chkRememberCredentials.Checked)
                accountSettings = new AccountSettings(txtEmail.Text, txtPassword.Text, chkRememberCredentials.Checked);
            
            _persistXml.LoadXml(AccountSettings.Serialize(accountSettings));

            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
